package com.downhometech.productcalculator.controllers;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.text.Editable;
import android.util.Log;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

import com.downhometech.productcalculator.models.Product;
import com.downhometech.productcalculator.models.Setting;

public class ProductController
{
    private Context context;
    
	private static final DecimalFormat doubleFormat = new DecimalFormat("##.00");
	public ProductController(Context context) {
		this.context = context;
	}
	
	public String getAndroidString(int id)
	{
		return context.getResources().getString(id);
	}
	
	public static String formatCurrency(double currency)
	{
		return "$" + doubleFormat.format(currency); 
	}
	
	public static String formatDouble(double value)
	{
		return "" + doubleFormat.format(value); 
	}
	
	public static String formatDouble(double value, String format)
	{
		DecimalFormat formatter = new DecimalFormat(format);
		return "" + formatter.format(value); 
	}
	
	public static boolean isDouble(String value)
	{
		try { Double.parseDouble(value); return true; } catch(Exception e) { return false; }
	}
	
	public static double getDouble(EditText textField)
	{
		String value = textField.getText().toString();
		if (isDouble(value)) { return Double.parseDouble(value); }
		else return -1;
	}
	
	private DBAdapter getOpenDatabaseAdapter()
	{
		DBAdapter databaseAdapter = new DBAdapter(context);
		databaseAdapter.open();
		return databaseAdapter;
	}
	
	private ArrayList<Product> getListOfProductsFromCursorWithClose(Cursor cursor)
	{
		ArrayList<Product> products = new ArrayList<Product>(); 
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            Product product = new Product(cursor);
            products.add(product);
            cursor.moveToNext();
        }
        cursor.close();
        return products;
	}
	
	private ArrayList<Setting> getListOfSettingsFromCursorWithClose(Cursor cursor)
	{
		ArrayList<Setting> settings = new ArrayList<Setting>(); 
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
        	Setting setting = new Setting(cursor);
            settings.add(setting);
            cursor.moveToNext();
        }
        cursor.close();
        return settings;
	}
	
	public ArrayList<Product> getAllProductsForCategory(int id)
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        ArrayList<Product> products = getListOfProductsFromCursorWithClose(databaseAdapter.getProductsForCategory(id));
        databaseAdapter.close();
        return products;
	}
	
	public ArrayList<Product> getAllCategories()
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        ArrayList<Product> products = getListOfProductsFromCursorWithClose(databaseAdapter.getProductCategoriesOrderedByName());
        databaseAdapter.close();
        return products;
	}
	
	public ArrayList<Product> getAllProducts()
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        ArrayList<Product> products = getListOfProductsFromCursorWithClose(databaseAdapter.getProductsOrderedByName());
        databaseAdapter.close();
        return products;
	}
	
	public ArrayList<Setting> getAllSettings()
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        ArrayList<Setting> settings = getListOfSettingsFromCursorWithClose(databaseAdapter.getSettings());
        databaseAdapter.close();
        return settings;
	}
	
	public void saveSetting(Setting setting, int id)
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        databaseAdapter.saveSetting(setting, id);
	}
	
	public void saveProduct(Product product, int id)
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        databaseAdapter.saveProduct(product, id);
	}
	
	public void deleteProduct(Product product)
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        databaseAdapter.deleteProduct(product);
	}
	
	public Setting getSettingById(int id)
	{
		Setting setting = null;
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        ArrayList<Setting> settings = getListOfSettingsFromCursorWithClose(databaseAdapter.getSettingsById(id));
        databaseAdapter.close();
        if (settings.size() > 0) { setting = settings.get(0); }
        return setting;
	}
	
	public ArrayList<Product> searchForProducts(String search)
	{
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        ArrayList<Product> products = getListOfProductsFromCursorWithClose(databaseAdapter.getProductsMatchingSearch(search));
        databaseAdapter.close();
        return products;
	}
	
	public Product getProductById(int id)
	{
		Product product = null;
        DBAdapter databaseAdapter = getOpenDatabaseAdapter();
        ArrayList<Product> products = getListOfProductsFromCursorWithClose(databaseAdapter.getProductById(id));
        databaseAdapter.close();
        if (products.size() > 0) { product = products.get(0); }
        return product;
	}
}
